package com.daxilong.starchen.player.utils;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;

import androidx.core.app.ActivityCompat;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;

import com.daxilong.starchen.player.R;
import com.daxilong.starchen.player.activity.AudioPlayerActivity;

public class NotificationUtils {

    private static final String CHANNEL_ID = "AUDIO_CHANNEL";
    private static final String CHANNEL_NAME = "AUDiO_MEDIA";

    // 创建通知渠道（Android 8.0+）
    public static void createNotificationChannel(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(
                    CHANNEL_ID,
                    CHANNEL_NAME,
                    NotificationManager.IMPORTANCE_DEFAULT
            );
            channel.setDescription("任务提醒通知");

            NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(channel);
        }
    }

    @SuppressLint("MissingPermission")
    public static void showNotification(Context context, String title, String message) {
        Intent intent = new Intent(context, AudioPlayerActivity.class);
        intent.putExtra("Notification",true);//从状态栏进入音乐播放页面
        PendingIntent pi = PendingIntent.getActivity(context,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
                .setSmallIcon(R.mipmap.notification_music_playing)  // 必须设置小图标
                .setContentTitle(title)                   // 通知标题
                .setContentText(message)                 // 通知内容
                .setOngoing(true)// 设置为持续通知（无法手动滑动清除）
                .setAutoCancel(false)  // 禁止点击后自动消失
                .setContentIntent(pi)
                .setPriority(NotificationCompat.PRIORITY_DEFAULT); // 优先级

        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
        notificationManager.notify(1, builder.build()); // `1` 是通知ID，可以自定义
    }

}
